/**
 * js
 */

$(function () {
	initialPage();
	getGrid();
});

function initialPage() {
	$(window).resize(function() {
		$('#dataGrid').bootstrapTable('resetView', {height: $(window).height()-56});
	});
}

function getGrid() {
	$('#dataGrid').bootstrapTableEx({
		url: '../../srblive/courses/list?_' + $.now(),
		height: $(window).height()-57,
		queryParams: function(params){
			params.keyword = vm.keyword;
			return params;
		},
        singleSelect: true,
		columns: [
			{checkbox: true},
			{field : "title", title : "标题", width : "100px"},
			// {field : "cover", title : "封面图", width : "100px"},
			// {field : "classs", title : "分类", width : "100px"},
			{field : "subtitle", title : "副标题", width : "100px"},
			{field : "summary", title : "摘要", width : "100px"},
			// {field : "content", title : "内容", width : "100px"},
			// {field : "sourceurl", title : "音视频地址", width : "100px"},
			// {field : "duration", title : "时长：秒数", width : "100px"},
			// {field : "seriesid", title : "系列课id", width : "100px"},
			// {field : "focus", title : "首页焦点", width : "100px"},
			{field : "status", title : "状态", width : "100px",
                formatter: function (value, row, index) {
                    return Utils.commonFmt(value,row,index,conts.liveXcxCourses.status,"上架状态");
                }
            },
			{field : "series", title : "课程类型", width : "100px"},
            // {field : "singlesold", title : "单买", width : "100px"},
            {field : "sort", title : "排序序号", width : "100px"},
            {field : "zan", title : "点赞数量", width : "100px"},
            {field : "collect", title : "收藏数量", width : "100px"},
            {field : "views", title : "浏览数量", width : "100px"},
            {field : "1", title : "操作", width : "160px",formatter:FormatterJS.course.moreOperate},
			// {field : "activities", title : "是否有活动", width : "100px"},
			// {field : "activitiesfee", title : "活动价", width : "100px"},
		]
	})
}

//更多按钮操作
function  btnAction(_this,code,id){
    // $(_this).find("option[value='']").attr("selected", "selected");
    switch(code){
        case '':
            return;
            break;
        case 'edit':
            vm.edit(id);
            break;
        //0：待上架 1：已上架  2：已下架  3：已删除
        case 'shangjia':
            vm.setStatus(1,id);
            break;
        case 'xiajia':
            vm.setStatus(2,id);
            break;
        case 'delete':
            vm.remove(id);
            break;
        default:break;
    }
    // setTimeout(function(){
    //     $(_this).find("option[value='']").attr("selected", "selected");
    //     console.info($(_this).find("option:first").val());
    // },500);
}

var vm = new Vue({
	el:'#dpLTE',
	data: {
		keyword: {
		    key:null
        }
	},
	methods : {
        load: function () {
            $('#dataGrid').bootstrapTable('refresh');
        },
        save: function () {
            dialogOpen({
                title: '新增',
                url: 'srblive_html/courses/add.html?_' + $.now(),
                width: '80%',
                height: '80%',
                yes: function (iframeId) {
                    top.frames[iframeId].vm.acceptClick();
                },
            });
        },
        edit: function (id) {
            var flag = false;
            if (id!=0) {
                flag = true;
            } else {
                var ck = $('#dataGrid').bootstrapTable('getSelections');
                if (checkedRow(ck)) {
                    flag = true;
                }
                id = ck[0].id;
            }
            if (flag) {
                dialogOpen({
                    title: '编辑',
                    url: 'srblive_html/courses/edit.html?_' + $.now(),
                    width: '80%',
                    height: '80%',
                    success: function (iframeId) {
                        top.frames[iframeId].vm.liveXcxCourses.id = id;
                        top.frames[iframeId].vm.setForm();
                    },
                    yes: function (iframeId) {
                        top.frames[iframeId].vm.acceptClick();
                    }
                });
            }
        },
        remove: function (id) {
            var ids = []
            var flag = false;
            if (id!=0) {
                flag = true;
                ids[0] = id;
            } else {
                var ck = $('#dataGrid').bootstrapTable('getSelections');
                if (checkedRow(ck)) {
                    flag = true;
                }
                if (checkedArray(ck)) {
                    $.each(ck, function (idx, item) {
                        ids[idx] = item.id;
                    });
                }
            }
            if (flag) {
                $.RemoveForm({
                    url: '../../srblive/courses/remove?_' + $.now(),
                    param: ids,
                    success: function (data) {
                        vm.load();
                    }
                });
            }
        },
        setStatus: function (status, id) {//上架、下架
            //var ck = $('#dataGrid').bootstrapTable('getSelections');
            // if (checkedRow(ck)) {
                //0：待上架 1：已上架  2：已下架  3：已删除
                // if (ck[0].status == status) {
                //     layer.msg("已经" + (status == 2 ? "下架" : "上架") + "!");
                //     return false;
                // }
                $.ConfirmFormEx({
                    msg: "确认" + (status == 2 ? "下架" : "上架") + '该点播么？',
                    url: '../../srblive/courses/setStatus?_' + $.now(),
                    param: {
                        id: id,
                        status: status
                    },
                    success: function (data) {
                        vm.load();
                    }
                });
            // }
        }
    }
})